package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.dao.PurchaseListDao;
import com.atguigu.jxc.dao.PurchaseListGoodsDao;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.GoodsCount;
import com.atguigu.jxc.service.PurchaseListGoodsService;
import com.atguigu.jxc.util.DateUtil;
import com.atguigu.jxc.util.StringUtil;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class PurchaseListGoodsServiceImpl implements PurchaseListGoodsService {

    @Autowired
    private PurchaseListDao purchaseListDao;
    @Autowired
    private PurchaseListGoodsDao purchaseListGoodsDao;
    @Autowired
    private GoodsDao goodsDao;
    private Gson gson = new Gson();
    @Override
    @Transactional
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr) {
        if(StringUtil.isNotEmpty(purchaseListGoodsStr)){
            purchaseList.setUserId(1);
            //添加进货单
            purchaseListDao.save(purchaseList);
            //获取单号
            Integer purchaseListId = purchaseListDao.getByNum(purchaseList.getPurchaseNumber());
            List<PurchaseListGoods> listGoods = gson.fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>() {}.getType());
            for (PurchaseListGoods listGood : listGoods) {
                listGood.setPurchaseListId(purchaseListId);
                log.info(listGood.toString());
                //添加进货的商品
                purchaseListGoodsDao.save(listGood);
                Goods good = goodsDao.getById(listGood.getGoodsId());
                //更新商品库存
                goodsDao.updateQuantity(listGood.getGoodsId(),good.getInventoryQuantity()+listGood.getGoodsNum());
            }

        }
        return new ServiceVO(400,"进货的商品不能为空");
    }

    @Override
    public List<PurchaseList> list(String purchaseNumber, Integer supplierId, Integer state, String sTime, String eTime) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        }catch (ParseException e) {
            e.printStackTrace();
        }
        return purchaseListDao.getList(purchaseNumber,supplierId,state,start,end);
    }

    @Override
    public List<PurchaseListGoods> goodsList(Integer purchaseListId) {
        return purchaseListGoodsDao.get(purchaseListId);
    }

    @Override
    @Transactional
    public ServiceVO delete(Integer purchaseListId) {
        List<PurchaseListGoods> listGoods = purchaseListGoodsDao.get(purchaseListId);
        if(null != listGoods && listGoods.size()>0){
            purchaseListGoodsDao.deleteByListId(purchaseListId);
        }
        purchaseListDao.deleteById(purchaseListId);
        return new ServiceVO(100,"请求成功");
    }

    @Override
    public void updateState(Integer purchaseListId) {
        PurchaseList purchaseList = purchaseListDao.get(purchaseListId);
        int state = purchaseList.getState().intValue();
        if(state == 1){
            purchaseListDao.updateState(purchaseListId,2);
        }else{
            purchaseListDao.updateState(purchaseListId,1);
        }
    }

    @Override
    public List<GoodsCount> count(String sTime, String eTime, Integer goodsTypeId, String codeOrName) {
        Date start = null;
        Date end = null;
        String format = "yyyy-MM-dd";
        try{
            start = DateUtil.StringToDate(sTime,format);
            end = DateUtil.StringToDate(eTime,format);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return purchaseListGoodsDao.selectCount(start,end,goodsTypeId,codeOrName);
    }

}
